<template>
    <rx-layout>

        <div slot="center">
            <rx-fit :isheader="isheader">
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <rx-list-title :title="exportI18n('export.processStatistics', '流程统计')"></rx-list-title>
                    <span class="search-btn-box" v-if="!isheader">
                <span class="search-btn" @click.stop="searchshow"><i
                    class="iconfont iconguolv"></i>{{ $ti18('rxList.leachBtn', '过滤') }}</span>
              </span>
                </div>

                <div btnalign="right" slot="searchbar" v-if="fitSearch" @search="search" @cancel="cancel"
                     @close="closeSearchbar">
                    <a-form :layout="colLayout">
                        <a-row :gutter="24">
                            <a-col :span="colSpan">
                                <a-form-item :label="$ti18('rxList.title','标题')">
                                    <a-input :placeholder="commonInput('rxList.title','标题','rxDecorator.input')"
                                             v-model="queryParam.Q_NAME__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.code','编码')">
                                    <a-input :placeholder="i18n('form.code','编码','input')"
                                             v-model="queryParam.Q_KEY__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="colSpan">
                                <a-form-item :label="i18n('form.status','状态')">
                                    <a-select style="width: 100%" :placeholder="i18n('form.status','状态','select')"
                                              v-model="queryParam.Q_STATUS__S_EQ">
                                        <a-select-option value="">{{ i18n('pleaseSelect', '请选择') }}</a-select-option>
                                        <a-select-option value="DRAFT">{{ i18n('draft', '草稿') }}</a-select-option>
                                        <a-select-option value="DEPLOY">{{ i18n('deploy', '发布') }}</a-select-option>
                                        <a-select-option value="INVALID">{{ i18n('invalid', '作废') }}</a-select-option>
                                    </a-select>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <rx-grid
                    ref="table"
                    bordered
                    :allowRowSelect="true"
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    url="/api-bpm/bpm/core/bpmDef/getBpmCount"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="['10','30','40']"
                    id-field="defId"
                    :loadDataOnstart="true"
                >
                    <span slot="exportApp">
                         {{ exportI18n('export.isExport', '导出') }}
                        <export-app/>
                    </span>
                    <span slot="status" slot-scope="{text}">
                  <a-tag
                      :color="text==='DEPLOYED' ? 'green' : 'red'">
                    <span v-if="text==='DEPLOYED'">{{ i18n('deploy', '发布') }}</span>
                    <span v-else-if="text==='DRAFT'">{{ i18n('draft', '草稿') }}</span>
                    <span v-else-if="text==='INVALID'">{{ i18n('invalid', '作废') }}</span>
                  </a-tag>
                </span>
                    <span slot="appId" slot-scope="{ text, record }">
                    {{ allApps[text] ? $i18(allApps[text].appName, '应用名称') : $ti18('rxList.noAppName', '无') }}
                </span>
                <span slot="action" slot-scope="{text, record}" class="actionIcons">
                    <span  @click.stop="openNodeDialog(record)">
                          {{$ti18('rxList.editBtn',"编辑")}}
                    </span>
                    <span @click="setIsCount(record)" :title="i18n('list.closeSta','关闭统计')">{{record.isCount == 0 ? i18n("list.enableSta","开启统计") : i18n("list.closeSta","关闭统计")}}</span>
                </span>
                </rx-grid>
            </rx-fit>
        </div>
    </rx-layout>

</template>

<script>
import BpmDefApi from '@/api/bpm/core/bpmDef'
import RxCategoryTreeeditor from "@/components/redxun/rx-category-treeeditor";
import {RxLayout, Util, RxFit, RxGrid, BaseList, RxUserComponent} from '@lowcode/jpaas-common-lib'
import BpmInstStart from "./BpmInstStart.vue";
import FlowUtil from "@/views/modules/bpm/js/FlowUtil.js";
import changeAppJs from "@/views/modules/share/changeApp";
import {mapState} from 'vuex';
import exportApp from "@/views/modules/share/exportApp";
import selectNodeDialog from "@/views/modules/bpm/core/SelectNodeDialog.vue";
import BpmDefNodeApi from "@/api/bpm/core/bpmDefNodeCount";

export default {
    name: 'BpmDefList',
    mixins: [BaseList, FlowUtil, changeAppJs],
    components: {
        RxLayout,
        RxFit,
        RxGrid,
        RxCategoryTreeeditor,
        RxUserComponent,
        exportApp
    },
    computed: {
        ...mapState({
            // 所属应用
            allApps: state => state.appSetting ? state.appSetting.allApps : {}
        }),
    },
    data() {
        return {
            // 查询参数
            queryParam: {},
            // 表头
            columns: [
                {
                    title: this.$ti18('rxList.index', '序号'),
                    type: 'indexColumn',
                    dataIndex: 'serial',
                    scopedSlots: {customRender: 'serial'},
                    width: 60
                },
                {
                    title: this.$ti18('rxList.title', '标题'),
                    dataIndex: 'name',
                    width: 200
                },
                {
                    title: this.i18n('form.code', '编码'),
                    dataIndex: 'key',
                    width: 120
                },
                {
                    title: this.i18n('form.status', '状态'),
                    dataIndex: 'status',
                    width: 70,
                    scopedSlots: {customRender: 'status'}
                },
                {
                    title: this.i18n('form.application', '所属应用'),
                    dataIndex: 'appId',
                    width: 140,
                    sortField: 'APP_ID_',
                    scopedSlots: {customRender: 'appId'}
                },
                {
                    title: this.$ti18('rxForm.createTime', '创建时间'),
                    dataIndex: 'createTime',
                    width: 120
                },
                {
                    title: this.$ti18('rxList.action', '操作'),
                    dataIndex: 'action',
                    scopedSlots: {customRender: 'action'},
                    width: 120
                }
            ],
            curTree: {},
            visible: false,
            confirmLoading: false,
            startUser: [],
            selectBpmDef: "",
            statisticConfig: {
                type: "static_bpm",
                table: "BPM_DEF",
                field: "TREE_ID_",
                whereConf: [{name: "IS_MAIN_", type: "string", dateFormat: "", op: "=", value: "YES"}]
            },
            exportApp: true
        }
    },
    created() {
        this.needFilterAppId();
    },
    methods: {
        exportI18n(name, text, key) {
            return this.$ti18(name, text, "appExport", key);
        },
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmDef", key);
        },
        commonInput(name, text, key) {
            let value = this.$ti18(name, text);

            let input = this.$ti18(key, text);
            return input + ' ' + value;
        },
        search() {
            this.$refs.table.loadData();
        },
        cancel() {
            this.resetSearch();
        },
        closeSearchbar() {
            //关闭过滤弹窗；（点击X触发）
            this.fitSearch = false;
        },
        handleOk() {
            if (this.startUser.length == 0 || !this.startUser[0].id) {
                this.$message.error(this.i18n('selectInitor', '请选择发起人！'))
                return;
            }
            this.visible = false;
            var self = this;
            Util.open({
                component: BpmInstStart,
                curVm: self,
                title: this.i18n('list.processSim', '流程仿真'),
                max: true,
                data: {
                    defId: this.selectBpmDef.defId,
                    preview: true,
                    startUserId: this.startUser[0].id
                }
            }, function (action) {
            });
        },
        setIsCount(record){
            let self_=this;
            this.$confirm({
                title: this.i18n("defNodeMsg.closeMsg1","你确定要关闭【")+record.name+this.i18n("defNodeMsg.closeMSg2","】的流程统计吗？"),
                okText: self_.$ti18('rxForm.ackBtn','确认'),
                cancelText: self_.$ti18('rxForm.cancelBtn','取消'),
                okType: 'danger',
                onOk() {
                    //点击确定，关闭流程统计
                    let param = {
                        defId: record.defId,
                        isCount: record.isCount == 1 ? 0 : 1
                    };
                    BpmDefApi.update(param).then(res => {
                        if (res.success) {
                            //关闭统计时删除流程节点统计表里的数据
                            BpmDefNodeApi.deleByDefId(record.defId)
                            self_.$message.success(res.message);
                            self_.loadData();
                        } else {
                            self_.$message.error(self_.exportI18n('export.settingFail','设置失败！'));
                        }
                    });
                },
            });
        },
        openNodeDialog(record){
            var self_=this
            var defId=record.defId
            BpmDefApi.getNodesByActDefId(record.actDefId).then(res => {
                var nodes = res.data;
                var conf = {
                    component: selectNodeDialog,
                    title: self_.i18n("defNodeMsg.selectNode","请选择要统计的流程节点"),
                    curVm: self_,
                    data: {nodes: nodes,defId:defId},
                    widthHeight: ['800px', '600px']
                };
                Util.open(conf, function (action,data) {
                    if (action != 'ok') {
                        return;
                    }
                    //记录那些开启统计的节点
                    BpmDefNodeApi.saveNodesCount(record.defId,record.actDefId,data)
                    self_.$message.success(self_.i18n("defNodeMsg.updated","更新成功"));
                });
            });
        }

    }
}
</script>
<style scoped>

</style>